package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.eneity.Category;
import com.itheima.eneity.Setmeal;
import com.itheima.serivce.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealSerivce setmealSerivce;

    @Autowired
    private SetmealDishSerivce setmealDishSerivce;

    @Autowired
    private CategorySerivce categorySerivce;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealSerivce.save(setmealDto);
        return R.success("新增套餐成功");
    }

//    /**
//     * 套餐信息的分页
//     * @param page
//     * @param pageSize
//     * @param name
//     * @return
//     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
//        构造分页构造器
        Page<Setmeal> pageInfo = new Page(page, pageSize);
        Page<SetmealDto> dishDtoPage = new Page();
//        构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //执行分页查询
        setmealSerivce.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(item, setmealDto);
            //分类ID
            Long categoryId = item.getCategoryId();
            log.info(categoryId.toString());
            //根据ID查询分类对象
            Category category1 = categorySerivce.getById(categoryId);
            log.info(category1.toString());
            log.info(category1.toString());
            log.info(category1.toString());
            log.info(category1.toString());
            String name1 = category1.getName();
            log.info(name1);
            setmealDto.setCategoryName(name1);
            String categoryName = category1.getName();
            setmealDto.setCategoryName(categoryName);

            return setmealDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealSerivce.updateWithFlavor(setmealDto);
        return R.success("修改套餐成功");
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getByid(@PathVariable Long id){
        log.info("根据id查询套餐信息:{}",id);
        SetmealDto byIdWithFlavor = setmealSerivce.getByIdWithFlavor(id);
        return R.success(byIdWithFlavor);
    }

    /**
     * 根据id删除套餐
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("id为:{}",ids);
        setmealSerivce.remove(ids);
        return R.success("删除成功");
    }
}
